﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SYS.Web.Runtime;
using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using ExpertMIS.Codes;
using System.Text;
using System.Data;

namespace ExpertMIS
{
    public partial class MyTrouble : System.Web.UI.Page
    {
        private void RefreshTroubleType()
        {
            SYS.Common.Broker.CodeManagerBroker codeBroker = new SYS.Common.Broker.CodeManagerBroker();
            DataTable table = codeBroker.GetCodeDataByName("TroubleType");
            if (table != null && table.Rows.Count > 0)
            {
                rblType.DataSource = table;
                rblType.DataTextField = "Name";
                rblType.DataValueField = "Code";
                rblType.DataBind();

                if(rblType.Items.Count > 0) rblType.SelectedIndex = 0;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            RefreshTroubleType();
            Title = "人才留言";
        }

        protected void btnSumbit_Click(object sender, EventArgs e)
        {
            TroubleBroker tblBroker = new TroubleBroker();
            TroubleEntity troubleEntity = new TroubleEntity();

            troubleEntity.Type = rblType.SelectedValue;
            troubleEntity.Title = txtTitle.Text;
            troubleEntity.Address = txtAddr.Text;
            troubleEntity.Content = txtOpinion.Text;
            troubleEntity.IsPassed = false;
            troubleEntity.Stamp = DateTime.Now;
            troubleEntity.Tel = txtPhone.Text;
            troubleEntity.UserName = txtName.Text;

            // 判断当前页面是否为注册用户
            StringBuilder sbScript = new StringBuilder();
            if (tblBroker.SaveEntity(troubleEntity, null).Code == 0)
            {
                sbScript.Append("$(document).ready(function(){alert('您的问题已经成功提交，我们将会尽快给您处理！');window.location='OrganizationService.aspx'});");
                Page.ClientScript.RegisterClientScriptBlock(GetType(), GetHashCode().ToString(), sbScript.ToString(), true);
            }
            else
            {
                sbScript.Append("$(document).ready(function(){alert('您的问题提交失败，请稍候重试！');});");
                Page.ClientScript.RegisterClientScriptBlock(GetType(), GetHashCode().ToString(), sbScript.ToString(), true);
            }
        }
    }
}
